//
// cocos2d Hello World example
// http://www.cocos2d-iphone.org
//

// Import the interfaces
#import "ColorGameScene.h"
#import "MainScene.h"
#import "ColorAnswerScene.h"
#import "SimpleAudioEngine.h"

enum {
	nRed = 0,
	nOrange = 1,
	nYellow = 2,
	nGreen = 3,
	nVerdant = 4,
	nBlue = 5,
	nPurple = 6,
};

extern int nColorLevel;
int nColorNumber = -1;
int nColorValue = -1;

extern BOOL isSound;
extern BOOL isNormal;

// HelloWorld implementation
@implementation ColorGameScene

+(id) scene
{
	// 'scene' is an autorelease object.
	Scene *scene = [Scene node];
	
	// 'layer' is an autorelease object.
	ColorGameScene *layer = [ColorGameScene node];
	
	// add layer as a child to scene
	[scene addChild: layer];

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
		nCount = 0;
		numberCount = 0;
		
		Sprite *background = [Sprite spriteWithFile:@"background.png"];
		[background setPosition:ccp(240, 160)];
		[self addChild:background z:0];
		
		// ask director the the window size
		CGSize size = [[Director sharedDirector] winSize];
		
		Sprite *sprite = [Sprite spriteWithFile:@"davinci.png"];
		sprite.position = ccp( 0, 50);
		[self addChild:sprite];		
		id rotateAction = [RotateBy actionWithDuration:8 angle:180*4];		
		id jumpAction = [JumpBy actionWithDuration:8 position:ccp(size.width,0) height:200 jumps:4];		
		id fordward = [Spawn actions:rotateAction, jumpAction, nil];		
		id backwards = [fordward reverse];
		id sequence = [Sequence actions: fordward, backwards, nil];
		id repeat = [RepeatForever actionWithAction:sequence];
		[sprite runAction:repeat];
		
		BitmapFontAtlas *labelTitle = [BitmapFontAtlas bitmapFontAtlasWithString:@"Back" fntFile:@"bitmapFontTest.fnt"];
		MenuItem *itemBack = [MenuItemLabel itemWithLabel:labelTitle target:self selector:@selector(BackToMain:)];
		Menu *menu = [Menu menuWithItems:itemBack, nil];
		menu.position = ccp(420, 30);
		[self addChild:menu];
		
		colorSprite[nRed] = [[Sprite spriteWithFile:@"red.png"] retain];
		colorSprite[nOrange] = [[Sprite spriteWithFile:@"orange.png"] retain];
		colorSprite[nYellow] = [[Sprite spriteWithFile:@"yellow.png"] retain];
		colorSprite[nGreen] = [[Sprite spriteWithFile:@"green.png"] retain];
		colorSprite[nVerdant] = [[Sprite spriteWithFile:@"verdant.png"] retain];
		colorSprite[nBlue] = [[Sprite spriteWithFile:@"blue.png"] retain];
		colorSprite[nPurple] = [[Sprite spriteWithFile:@"purple.png"] retain];
		
		for (int i=0; i<7; i++) {
			[colorSprite[i] setPosition:ccp(240, 120)];
			[self addChild:colorSprite[i] z:i tag:i];
			[colorSprite[i] setOpacity:0];
		}
		
//		BitmapFontAtlas *label = [BitmapFontAtlas bitmapFontAtlasWithString:@"" fntFile:@"bitmapFontTest.fnt"];
//		[self addChild:label z:0 tag:1];
//		label.position = ccp(240, 140);
		
		BitmapFontAtlas *label1 = [BitmapFontAtlas bitmapFontAtlasWithString:@"" fntFile:@"bitmapFontTest.fnt"];
		[self addChild:label1 z:0 tag:7];
		label1.position = ccp(240, 180);
		
		NSString *string = [NSString stringWithFormat:@"Level %d", nColorLevel];
		BitmapFontAtlas *labelLevel = [BitmapFontAtlas bitmapFontAtlasWithString:string fntFile:@"bitmapFontTest.fnt"];
		[self addChild:labelLevel z:0 tag:8];
		labelLevel.position = ccp(80, 280);
	
		float td;
		if (isNormal == YES) {
			td = 2.0f;
		}
		else {
			td = 1.0f;
		}

		[self schedule:@selector(step:) interval: td];
	}
	return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	for (int i=0; i<7; i++) {
		[colorSprite[i] release];
	}
	[super dealloc];
}

- (void) BackToMain: (id) sender
{
	[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[MainScene node]]];
}

-(void) step:(ccTime) dt
{
	nCount++;
	if (nCount == 1) {
		BitmapFontAtlas *label1 = (BitmapFontAtlas*) [self getChildByTag:7];
		[label1 setString:@"ready"];
//		nNumber = (int)(CCRANDOM_0_1() * (nLevel + 4)) + 1;
		nColorNumber = 1 + arc4random() % (nColorLevel + 4);
		if (isSound == YES) {
			[[SimpleAudioEngine sharedEngine] playEffect:@"ready.wav"];
		}
	}
	if (nCount == 2) {
		BitmapFontAtlas *label1 = (BitmapFontAtlas*) [self getChildByTag:7];
		[label1 setString:@"Go"];
		//		nNumber = (int)(CCRANDOM_0_1() * (nLevel + 4)) + 1;
		nColorNumber = 1 + arc4random() % (nColorLevel + 4);
		if (isSound == YES) {
			[[SimpleAudioEngine sharedEngine] playEffect:@"go.wav"];
		}
	}
	if (nCount > 2 && nCount < nColorLevel + 7) {
		numberCount++;
//		int number = (int)(CCRANDOM_0_1() * 10);
		int number = arc4random() % 7;
//		NSString *string = [NSString stringWithFormat:@"%d", number];
//		BitmapFontAtlas *label = (BitmapFontAtlas*) [self getChildByTag:1];
//		[label setString:string];
		
		for (int i=0; i<7; i++) {
			Sprite *sprite = (Sprite*)[self getChildByTag:i];
			[sprite setOpacity:0];
		}
		
		Sprite *showSprite = (Sprite*)[self getChildByTag:number];
		[showSprite setOpacity:255];
		
		BitmapFontAtlas *label1 = (BitmapFontAtlas*) [self getChildByTag:7];
		NSString *string1 = [NSString stringWithFormat:@"No.%d", numberCount];
		[label1 setString:string1];
		if (numberCount == nColorNumber) {
			nColorValue = number;
		}
		
		if (isSound == YES) {
			switch (number) {
				case nRed:
					[[SimpleAudioEngine sharedEngine] playEffect:@"red.wav"];
					break;
				case nOrange:
					[[SimpleAudioEngine sharedEngine] playEffect:@"orange.wav"];
					break;
				case nYellow:
					[[SimpleAudioEngine sharedEngine] playEffect:@"yellow.wav"];
					break;
				case nGreen:
					[[SimpleAudioEngine sharedEngine] playEffect:@"green.wav"];
					break;
				case nVerdant:
					[[SimpleAudioEngine sharedEngine] playEffect:@"verdant.wav"];
					break;
				case nBlue:
					[[SimpleAudioEngine sharedEngine] playEffect:@"blue.wav"];
					break;
				case nPurple:
					[[SimpleAudioEngine sharedEngine] playEffect:@"purple.wav"];
					break;
				default:
					break;
			}
		}
	}
	if (nCount == nColorLevel + 7) {
		for (int i=0; i<7; i++) {
			Sprite *sprite = (Sprite*)[self getChildByTag:i];
			[sprite setOpacity:0];
		}
//		BitmapFontAtlas *label = (BitmapFontAtlas*) [self getChildByTag:1];
//		[label setString:@""];
		BitmapFontAtlas *label1 = (BitmapFontAtlas*) [self getChildByTag:7];
		[label1 setString:@""];
		[[Director sharedDirector] replaceScene:[FlipXTransition transitionWithDuration:1 scene:[ColorAnswerScene node]]];
	}

}
@end
